<?php

/*
 * ************************************************* 
 * Created on :2012-5-2 13:18:02
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class News {

    static $allow_type = array('河工新闻', '校友会快讯', '人物动态', '领导关怀',
        '捐赠事迹', '年度捐赠', '校庆精选文稿汇编', '分会新闻', '讲堂动态', '最新服务', '校友通讯');
    static $table = 'news';

    public static function bycondition($condition, $offset, $size, $order = "newsid desc") {
        $r = DB::LimitQuery(self::$table, array("condition" => $condition, "offset" => $offset, "size" => $size, "order" => "ORDER BY " . $order));
        return $r;
    }

    public static function countbycondition($condition) {
        $r = DB::LimitQuery(self::$table, array("condition" => $condition, "select" => 'count(*) as count'));
        return $r[0]['count'];
    }

    /**
     * 获取一个类型的新闻
     * @param type $type
     * @return boolean 
     */
    public static function bytype($type, $offset = 0, $size = PAGE_SIZE) {
        $r = self::bycondition(array("newstype" => $type), $offset, $size);
        return $r;
    }

    /**
     * 获取对应分会的新闻列表
     * @param type $remark 
     */
    public static function byplace($remark, $offset = 0, $size = PAGE_SIZE) {
        if (empty($remark))
            return false;
        $r = self::bycondition(array("newstype" => "分会新闻", "remark" => $remark), $offset, $size);
        return $r;
    }

    /**
     * 根据新闻的id获取
     * @param type $newsid
     * @return boolean 
     */
    public static function byid($newsid) {
        if (empty($newsid)) {
            return false;
        }
        $r = self::bycondition(array("newsid" => $newsid), 0, 1);
        return empty($r) ? $r : $r[0];
    }

    /**
     * 添加新闻,如果是分会新闻的话那么remark要设置成对应的分会id
     * @param type $type
     * @param type $title
     * @param type $content
     * @param type $remark
     * @return boolean 
     */
    public static function add($type, $title, $content, $remark = NULL) {
        if (!in_array($type, self::$allow_type) || empty($title) || empty($content)
                || ($type == '分会新闻' && empty($remark)))
            return false;
        else {
            $data = array("newstype" => $type, "title" => $title, "content" => $content,
                "userid" => $GLOBALS['user']->userid, "remark" => $remark, "createtime" => date("Y-m-d H:i:s"));
            $r = DB::Insert(self::$table, $data);
            return $r;
        }
    }

    /**
     * 修改信息
     * @param type $newsid
     * @param type $data
     * @return boolean 
     */
    public static function modify($newsid, $data) {
        if (empty($newsid) || !is_array($data))
            return false;
        foreach ($data as $k => $v) {
            if (!in_array($k, array("newstype", "title", "content", "remark")))
                return false;
            if ($k == 'type' && !in_array($v, self::$allow_type))
                return false;
        }
        return DB::Update(self::$table, $newsid, $data, "newsid");
    }

    /**
     * 删除
     * @param type $newsid
     * @return boolean 
     */
    public static function del($newsid) {
        if (empty($newsid))
            return false;
        return DB::Delete(self::$table, array("newsid" => $newsid));
    }

}

/* End of file News */


